package com.train.mgr.config.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


@Configuration
@EnableConfigurationProperties(ThreadPoolProperties.class)
@Slf4j
public class ThreadPoolTaskExecutorConfig {

    public ThreadPoolTaskExecutorConfig() {
    }

    @Bean(name = {"taskExecutor"})
    public ThreadPoolTaskExecutor taskExecutor(ThreadPoolProperties properties) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(properties.getCorePoolSize());
        executor.setMaxPoolSize(properties.getMaximumPoolSize());
        executor.setQueueCapacity(properties.getCapacity());
        executor.setKeepAliveSeconds(properties.getKeepAliveTime());
        //executor.setRejectedExecutionHandler((RejectedExecutionHandler) Class.forName(rejectPolicy).newInstance());
        executor.setThreadNamePrefix(properties.getThreadNamePrefix());

        executor.initialize();
        return executor;
    }
}
